package org.aldar.bquest.util;

import android.util.Log;

/**
 * User: Daria
 * Date: 9/10/13 12:04 AM
 */
public class Logger {

    private Class clazz;

    private boolean logEnabled = true;
    private boolean debugEnabled = false;

    public Logger(Class clazz) {
        this.clazz = clazz;
    }

    public void i(String str) {
        if (logEnabled) {
            Log.i("loonyrocket" + clazz.getCanonicalName().substring(clazz.getCanonicalName().lastIndexOf(".")), str);
        }
    }

    public void i(String str1, String str) {
        if (logEnabled) {
            Log.i("loonyrocket" + clazz.getCanonicalName().substring(clazz.getCanonicalName().lastIndexOf(".")), str1 + " --- " + str);
        }
    }

    public void d(String str) {
        if (logEnabled && debugEnabled)
            Log.d("loonyrocket" + clazz.getCanonicalName().substring(clazz.getCanonicalName().lastIndexOf(".")), str);
    }

    public void e(String s, Throwable e) {
        if (logEnabled)
            Log.e("loonyrocket" + clazz.getCanonicalName().substring(clazz.getCanonicalName().lastIndexOf(".")), s + " Exception text: " + e.getMessage(), e);
    }

    public void e(String str) {
        if (logEnabled)
            Log.e("loonyrocket" + clazz.getCanonicalName().substring(clazz.getCanonicalName().lastIndexOf(".")), str);
    }

    public String getCallerStr(int callerCount) {
        StringBuilder result = new StringBuilder();
        StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
        if (callerCount > 0)
            result.append("caller1: " + stackTraceElements[5].getClassName() +
                    "." + stackTraceElements[5].getMethodName() +
                    " line " + stackTraceElements[5].getLineNumber());
        if (callerCount > 1)
            result.append(", caller2: " + stackTraceElements[6].getClassName() +
                    "." + stackTraceElements[6].getMethodName() +
                    " line " + stackTraceElements[6].getLineNumber());
        if (callerCount > 2)
            result.append(", caller3: " + stackTraceElements[7].getClassName() +
                    "." + stackTraceElements[7].getMethodName() +
                    " line " + stackTraceElements[7].getLineNumber());
        if (callerCount > 3)
            result.append(", caller4: " + stackTraceElements[8].getClassName() +
                    "." + stackTraceElements[8].getMethodName() +
                    " line " + stackTraceElements[8].getLineNumber());
        return result.toString();
    }

    public void i(String logStr, int callerCount) {
        if (logEnabled) {
            i(logStr + "(" + getCallerStr(callerCount) + ")");
        }
    }
}
